<?php

/**

 * PHPSense Pagination Class

 *

 * PHP tutorials and scripts

 *

 * @package		PHPSense

 * @author		Jatinder Singh Thind

 * @copyright	Copyright (c) 2006, Jatinder Singh Thind

 * @link		http://www.phpsense.com

 */



// ------------------------------------------------------------------------





class Pagination extends db{
	var $php_self;
	var $rows_per_page = 10; //Number of records to display per page
	var $total_rows = 0; //Total number of rows returned by the query
	var $links_per_page = 5; //Number of links to display per page
	var $append = ""; //Paremeters to append to pagination links
	var $sql = "";
	var $debug = false;
	var $conn = false;
	var $page = 1;
	var $max_pages = 0;
	var $offset = 0;
	var $cache_time;
	var $ext="";

	function Pagination($sql, $rows_per_page = 10, $links_per_page = 5, $append = "",$time=10,$ext="",$page='') {
		$this->ext=$ext;
		$this->cache_time=$time;
		$this->sql = $sql;
		$this->rows_per_page = (int)$rows_per_page;
		if (intval($links_per_page ) > 0) {
			$this->links_per_page = (int)$links_per_page;
		} else {
			$this->links_per_page = 5;
		}
		$this->append = $append;
		$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
		if (isset($_GET['page'] )) {
			$this->page = intval($_GET['page'] );
		}

	}

	

	/**
	 * Executes the SQL query and initializes internal variables
	 *
	 * @access public
	 * @return resource
	 */
	function paginate($cache,$sql2) {
		//Load data from cache
		    // 1.Load Tong So Record
			$data=$cache->load($sql2,"pagination");
			if($data)
			{
			//------Lay tong so dong va ket qua;
			$this->total_rows=$data;
			}
			else
			{
				// 1. Load Result du tren limit

		//Neu load ko duoc se ket noi co so du lieu va luu data vao cache========
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
			if($this->cache_time>0){
				$cache->save($sql2,$this->total_rows,$this->cache_time,'pagination');

			}
		}

		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
			if ($this->debug)
			return FALSE;
		}
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
			$this->links_per_page = $this->max_pages;
		}
		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}
		//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " limit {$this->offset},{$this->rows_per_page}";
		$data2=$cache->load($sql,'pagination');
		if($data2)
		{	
			return $data2;
		}
		else
		{ 
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		if($this->ext=='post'){ // Truong hop dung de query lay post image,video,text from indexpost wwhere ext=post
					while($row=$this->fetch())
					{

							if($row['is_image']==1){
									 $dulieu=cache_get_post($row['post_id'],"post_image",$row);
							}
							if($row['is_video']==1){
								$data3[$i++]=cache_get_post($row['post_id'],"post_media",$row);

						}
					if($row['is_text']==1){
									$dulieu=cache_get_post($row['post_id'],"post_text",$row);
									$val=array();
									$val['pid']=$row['id'];
									$val['title']=$dulieu['title'];
									$val['alias']=$dulieu['alias'];
									$val['username']=$dulieu['username'];
									$val['views']=$dulieu['views'];
									$val['cat_id']=$dulieu['cat_id'];
									$val['sub_id']=$dulieu['sub_id'];
									$val['image']=$dulieu['image'];
									$val['sdes']=$dulieu['sdes'];
									$val['url']=$dulieu['url'];
									$val['category']=$dulieu['category'];
									$val['subcategory']=$dulieu['subcategory'];
									//Sub va cat id minh lay tile,url la oke
								$data3[$i++]=$val;
						}
		

					}

		}else

		if($this->ext=='album'){
				while($row=$this->fetch())
					{
					//query get link image -----
							$image=album_get_image($row['id']);
							$row['image']=$image;
						$data3[$i++]=$row;
					}

		}else

		if($this->ext=='truyen_1'){
				while($row=$this->fetch()){
					$truyen=cache_get_truyen($row['id']);
					$rc['id']=$truyen['id'];
					$rc['ten']=$truyen['ten'];
					$rc['alias']=$truyen['alias'];
					$rc['hinhanh']=$truyen['hinhanh'];
					$rc['theloai']=$truyen['theloai'];
					$rc['theloai2']=$truyen['theloai2'];
					$rc['tacgia']=$truyen['tacgia'];
					$rc['nguon']=$truyen['nguon'];
					$rc['chuongmoinhat']=$truyen['chuongmoinhat'];
					$rc['chuongmoinhat_url']=$truyen['chuongmoinhat_url'];
					$rc['chuongbatdau']=$truyen['chuongbatdau'];
					$rc['chuongbatdau_url']=$truyen['chuongbatdau_url'];
					$rc['sochuong']=$truyen['sochuong'];
					$rc['views']=$truyen['views'];
					$rc['tinhtrang']=$truyen['tinhtrang'];
					$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
					$rc['time_upload']=$truyen['time_upload'];
					$rc['user_upload']=$truyen['user_upload'];
					$rc['pick']=$truyen['pick'];
					$rc['rate_average'] = $truyen['rate_average'];
					$data3[$i++]=$rc;

				}

		}else //End of ext truyen

		if($this->ext=='truyen_2'){ // GET TRUYEN BY TAG THE LOAI

				while($row=$this->fetch()){
					$truyen=cache_get_truyen($row['truyen_id']);
					$rc['id']=$truyen['id'];
					$rc['ten']=$truyen['ten'];
					$rc['alias']=$truyen['alias'];
					$rc['hinhanh']=$truyen['hinhanh'];
					$rc['tacgia']=$truyen['tacgia'];
					$rc['theloai']=$truyen['theloai'];
		  		   $rc['theloai2']=$truyen['theloai2'];
				   $rc['nguon']=$truyen['nguon'];
					$rc['chuongmoinhat']=$truyen['chuongmoinhat'];
					$rc['chuongmoinhat_url']=$truyen['chuongmoinhat_url'];
					$rc['chuongbatdau']=$truyen['chuongbatdau'];
					$rc['chuongbatdau_url']=$truyen['chuongbatdau_url'];
					$rc['sochuong']=$truyen['sochuong'];
					$rc['tinhtrang']=$truyen['tinhtrang'];
					$rc['tinhtrang-text']=$truyen['tinhtrang-text'];
					$rc['views']=$truyen['views'];
					$rc['time_upload']=$truyen['time_upload'];
					$rc['user_upload']=$truyen['user_upload'];
					$rc['pick']=$truyen['pick'];
					$rc['rate_average'] = $truyen['rate_average'];
					$data3[$i++]=$rc;

				}

		}else //End of ext truyen
		{
			// Lay data phan trang paging binh thuong
				while($row=$this->fetch())
					{
							$data3[$i++]=$row;
					}

		}

		

		//End of lay du lieu, close ket noi

		$this->close();
				if($this->cache_time>0){
					$cache->save($sql,$data3,$this->cache_time,'pagination');
				}
		return $data3;
		} //end of not cache condtion
	}

	

		function paginate_nocache($sql2) {
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
		if ($this->debug)
			return FALSE;
		}
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
		$this->links_per_page = $this->max_pages;
		}

		

		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}

			//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}";
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		while($row=$this->fetch())
		{
		$data3[$i++]=$row;
		}
		$this->close();
		return $data3;
		} //end of not cache condtion

	function renderOlderpost($tag = 'Older Post') {
		if ($this->total_rows == 0)
			return FALSE;
		if ($this->page < $this->max_pages) {
			return '
				<ul class="pager">
 			 <li class="previous">
			<a href="' . $this->append . ($this->page + 1) .'">&larr;' . $tag . '</a>
			</li>
			 </ul>
			';

		} else {
			return false;
		}
	}

	function renderNewerpost($tag = 'Newer Post') {
		if ($this->total_rows == 0)
			return FALSE;

		if ($this->page > 1) {
			return ' <ul class="pager">
 			 <li class="next"><a href="' . $this->append . ($this->page - 1) .'">' . $tag . ' &rarr;</a></li>
			 </ul>+';
		} else {
			return false;
		}

	}

	 

	function renderFirst($tag = '&lsaquo;&lsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;

		if ($this->page == 1) {
			return false;
		} else {

		return '
			<li ><a href="' . $this->append .'1'.'">' . $tag . '</a></li>';
		}

	}

	function renderLast($tag = '&rsaquo;&rsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;

		if ($this->page == $this->max_pages) {
			return false;
		} else {

			return '<li  ><a href="' . $this->append.$this->max_pages .'">' . $tag . '</a></li>';

		}

	}


	function renderNext($tag = '&rsaquo;') {

		if ($this->total_rows == 0)

			return FALSE;

		

		if ($this->page < $this->max_pages) {

			return '

			<li ><a href="' . $this->append.($this->page + 1) .''.'">' . $tag . '</a></li>';

		} else {

			return false;

		}

	}

	

	/**

	 * Display the previous link

	 *

	 * @access public

	 * @param string $tag Text string to be displayed as the link. Defaults to '<<'

	 * @return string

	 */

	function renderPrev($tag = '&lsaquo;') {

		if ($this->total_rows == 0)

			return FALSE;

		

		if ($this->page > 1) {

			return '

			<li ><a href="' . $this->append .($this->page - 1) .''.'">' . $tag . '</a></li>';



		} else {

			return false;

		}

	}

	

	/**

	 * Display the page links

	 *

	 * @access public

	 * @return string

	 */

	function renderNav($prefix = '<li >', $suffix = '</li> ') {

		if ($this->total_rows == 0)

			return FALSE;

		

		$batch = ceil($this->page / $this->links_per_page );

		$end = $batch * $this->links_per_page;

		if ($end == $this->page) {

			//$end = $end + $this->links_per_page - 1;

		//$end = $end + ceil($this->links_per_page/2);

		}

		if ($end > $this->max_pages) {

			$end = $this->max_pages;

		}

		$start = $end - $this->links_per_page + 1;

		$links = '';

		

		for($i = $start; $i <= $end; $i ++) {

			if ($i == $this->page) {

				$links .=  "<li  class='active'><span>" . $i . $suffix."</span>";

			} else {

				$links .= ' ' . $prefix . '<a href="' . $this->append . $i . '">' . $i . '</a>' . $suffix . ' ';

			}

		}

		

		return $links;

	}

	

	/**

	 * Display full pagination navigation

	 *

	 * @access public

	 * @return string

	 */

	function renderFullNav() {
	if($this->total_rows>$this->rows_per_page)
		return "<div><ul class='pagination'><li  class='disabled'><span>Page($this->max_pages)</span></li> ".$this->renderFirst() .$this->renderPrev(). $this->renderNav() . $this->renderNext().$this->renderLast()."</ul></div>";

	}

		function renderFullNav2() {
	if($this->total_rows>$this->rows_per_page)
		return "<div><ul class='pagination'>".$this->renderFirst() .$this->renderPrev(). $this->renderNav() . $this->renderNext().$this->renderLast()."</ul></div>";

	}

	/**

	 * Set debug mode

	 *

	 * @access public

	 * @param bool $debug Set to TRUE to enable debug messages

	 * @return void

	 */

	function setDebug($debug) {

		$this->debug = $debug;

	}

	

	function get_num_page(){

	return $this->max_pages;

	}

}

?>

